package com.makenv.web.portal;

import com.makenv.domain.Article;
import com.makenv.domain.Video;
import com.makenv.service.ArticleService;
import com.makenv.service.VideoService;
import com.makenv.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/video")
public class VideoController {

    @Autowired
    VideoService videoService;

    @Autowired
    ArticleService articleService;

    // 每页显示的行数
    private Integer row;

    /**
     * @Author : lilimin
     * @Description : 显示前台视频的书籍
     * @Date : Created in 14:47 2017/12/4
     */

    @RequestMapping("/showfront")
    public String showFront(HttpServletRequest request) {
        forShow(request);
        return "front/resource";
    }

    @RequestMapping("/jump")
    public String jump(@RequestParam int pageNum, @RequestParam(required = false) String content, HttpServletRequest request) {

        int pageSum = 0;
        if (content == null) {
            pageSum = getPageSum();
        } else {
            pageSum = getSearchPageSum(content);
        }
        pageNum = pageNum <= -1 ? 1 : pageNum;
        List<Video> videoList = videoService.showVideoList((pageNum - 1) * row, row, content);
        request.getSession().setAttribute("pageSum", pageSum);
        request.getSession().setAttribute("pageNum", pageNum);
        request.setAttribute("videoList", videoList);
        return "front/resource";
    }

    @RequestMapping("/search")
    public String search(HttpServletRequest request, @RequestParam("name") String bookName) {
        int pageSum = getSearchPageSum(bookName);
        request.getSession().setAttribute("pageSum", pageSum);
        request.getSession().setAttribute("pageNum", 1);
        request.getSession().setAttribute("content", bookName);
        List<Video> videoList = videoService.showVideoList(0, row, bookName);
        request.setAttribute("videoList", videoList);
        return "front/resource";
    }

    public void forShow(HttpServletRequest request) {
        row = Config.getInt("showRow");
        List<Video> videoList = videoService.showVideoList(0, row, null);
        List<Article> articleList = articleService.showArticleList();
        String notice = articleService.showNotice();
        int pageSum = getPageSum();
        request.getSession().setAttribute("pageSum", pageSum);
        request.getSession().setAttribute("pageNum", 1);
        request.setAttribute("videoList", videoList);
        request.getSession().setAttribute("articleList", articleList);
        request.getSession().setAttribute("notice", notice);
    }

    public Integer getPageSum() {
        int pageSum;
        int totalBook = videoService.getVideoSum();
        if (totalBook % row == 0) {
            pageSum = totalBook / row;
        } else {
            pageSum = totalBook / row + 1;
        }
        return pageSum;
    }

    public Integer getSearchPageSum(String bookName) {
        int pageSum;
        int totalBook = videoService.getSearchVideoSum(bookName);
        if (totalBook % row == 0) {
            pageSum = totalBook / row;
        } else {
            pageSum = totalBook / row + 1;
        }
        return pageSum;
    }
}
